//一般直接写在一个js文件中
layui.use(['element', 'form', 'laydate', 'upload'], function () {
    var form = layui.form,
        upload = layui.upload,
        laydate = layui.laydate,
        element = layui.element;
    laydate.render({
        elem: '#birthday' //指定元素
    });
    $("#avatarimg").on('click', function () {
        layer.open({
            title: '头像上传编辑器',
            type: 2
            , content: '/static/plugins/fullAvatarEditor/avatarupload.html'
            , area: ['700px', '500px']
        });
    });

//省选择回调
    form.on('select(province)', function (data) {
        var name = $(data.elem).find("option:selected").text();
        $("#provinceName").val(name);
        $("#provinceCode").val(data.value);
        $.ajax({
            url: "/city?pId=" + data.value,
            type: 'get',
            dataType: 'json',
            success: function (rs) {
                if (rs) {
                    $("#city").empty().append('<option value="">请选择市</option>');
                    $.each(rs, function (i, d) {
                        $("#city").append('<option value="' + d.id + '">' + d.name + '</option>');
                    });
                    form.render('select'); //刷新select选择框渲染
                }
            }
        });
    });

    //市选择回调
    form.on('select(city)', function (data) {
        var name = $(data.elem).find("option:selected").text();
        $("#cityName").val(name);
        $("#cityCode").val(data.value);
        $.ajax({
            url: "/area?pId=" + data.value,
            type: 'get',
            dataType: 'json',
            success: function (rs) {
                if (rs) {
                    $("#area").empty().append('<option value="">请选择区(县)</option>');
                    $.each(rs, function (i, d) {
                        $("#area").append('<option value="' + d.id + '">' + d.name + '</option>');
                    });
                    form.render('select'); //刷新select选择框渲染
                }
            }
        });
    });

    //区选择回调
    form.on('select(area)', function (data) {
        var name = $(data.elem).find("option:selected").text();
        $("#areaName").val(name);
        $("#areaCode").val(data.value);
        $.ajax({
            url: "/town?pId=" + data.value,
            type: 'get',
            dataType: 'json',
            success: function (rs) {
                if (rs) {
                    $("#town").empty().append('<option value="">请选择乡镇(街道)</option>');
                    $.each(rs, function (i, d) {
                        $("#town").append('<option value="' + d.id + '">' + d.name + '</option>');
                    });
                    form.render('select'); //刷新select选择框渲染
                }
            }
        });
    });
    //镇选择回调
    form.on('select(town)', function (data) {
        var name = $(data.elem).find("option:selected").text();
        $("#townName").val(name);
        $("#townCode").val(data.value);
        $.ajax({
            url: "/village?pId=" + data.value,
            type: 'get',
            dataType: 'json',
            success: function (rs) {
                if (rs) {
                    $("#village").empty().append('<option value="">请选择社区(村)</option>');
                    $.each(rs, function (i, d) {
                        $("#village").append('<option value="' + d.id + '">' + d.name + '</option>');
                    });
                    form.render('select'); //刷新select选择框渲染
                }
            }
        });
    });


    //村选择回调
    form.on('select(village)', function (data) {
        var name = $(data.elem).find("option:selected").text();
        $("#villageName").val(name);
        $("#villageCode").val(data.value);
    });


    //表单验证
    form.verify({
        only: function (value, item) { //value：表单的值、item：表单的DOM对象
            var flag = false;
            var idNo = $("#idNo").val();
            $.ajax({
                url: "/enterElderly/exists",
                data: {idNo: idNo},
                dataType: "json",
                async: false,
                type: "post",
                success: function (rs) {
                    if (rs.code == 200) {
                        flag = rs.data;
                    }
                }
            });
            if (flag) {
                return $(item).data("label") + '已存在';
            }
        }
    });

    element.on('tab(demo)', function (data) {
        if (data.index != 0) {
            layer.msg("请先保存老人基本信息");
            element.tabChange('demo', 'one');
        }
    });

    $("#idNo").blur(function () {
        var idNo = $(this).val();
        var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        if (reg.test(idNo) === false) {
            layer.msg("请填写正确的身份证号");
            $("#birthday").val("")
        } else {
            if (idNo.length == 15) {
                var s = idNo.substring(0, 6);
                var s1 = idNo.substring(6, 15);
                idNo = s + "19" + s1;
            }
            $("#birthday").val(idNo.substring(6, 10) + "-" + idNo.substring(10, 12) + "-" + idNo.substring(12, 14))
        }

    })
});